package com.qiku.controller;

import com.qiku.domin.*;
import com.qiku.service.MenuService;
import com.qiku.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;


    @RequestMapping(value = "/findAllRole", method = RequestMethod.POST)
    public ResponseResult findAllRole(@RequestBody RoleVO roleVO) {

        try {

            List<Role> allRole = roleService.findAllRole(roleVO);
            ResponseResult result = new ResponseResult(true, 200, "相应成功", allRole);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ResponseResult result = new ResponseResult(false, 201, "错误", null);
            return result;
        }


    }


    @GetMapping("/findSubMenuListByPid")
    public ResponseResult findSubMenuListByPid() {
        // -1 表示查询所有 根据数据库设计决定
        List<Menu> menuList = menuService.findSubMenuListByPid(-1);
        Map<String, Object> map = new HashMap<>();
        map.put("parentMenuList", menuList);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
        return result;
    }


    @PostMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(@RequestBody Role role) {
        if (null != role.getId() && 0 < role.getId()) {
            // 修改
            roleService.UpdateRole(role);
            ResponseResult result = new ResponseResult(true, 200, "修改成功", null);
            return result;
        } else {
            roleService.saveRole(role);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", null);
            return result;
        }
    }

    @GetMapping("/findMenuByRoleId")
    public ResponseResult findMenuBuRoleId(@RequestParam Integer roleId) {
        List<String> menuBuRoleId = roleService.findMenuBuRoleId(roleId);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", menuBuRoleId);
        return result;
    }

    @PostMapping("/RoleContextMenu")
    public ResponseResult RoleContextMenu(@RequestBody RoleMenuVo roleMenuVo){
        roleService.RoleContextMenu(roleMenuVo);
        ResponseResult result = new ResponseResult(true, 200, "相应成功", null);
        return result;
    }


    @GetMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id){
        roleService.deeteRole(id);
        ResponseResult result = new ResponseResult(true, 200, "相应成功", null);
        return result;
    }

    @GetMapping("/findAllMenu")
    public ResponseResult findAllMenu(){
        List<Menu> list = menuService.findSubMenuListByPid(-1);
        Map<String,Object> map = new HashMap<>();
        map.put("parentMenuList",list);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
        return result;
    }


}
